package com.zzw.service.impl;

import com.zzw.bean.*;
import com.zzw.dao.IClassDao;
import com.zzw.dao.IOrderFormDao;
import com.zzw.dao.ITeacherDao;
import org.springframework.stereotype.Service;
import com.zzw.service.IOrderFormService;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by 张志伟 on 2017/11/22.
 */
@Service
public class OrderFormServiceImpl implements IOrderFormService {
    @Resource
    private IOrderFormDao orderformDao;
    @Resource
    private IClassDao classDao;
    @Resource
    private ITeacherDao teacherDao;


    /**
     * desc：通过用户ID获取订单实体
     */
    public ArrayList<OrderFormVo> getOrderFormByUid(Integer uid) {
        //ArrayList<Integer> cidList = orderformDao.getOrderFormCidByUid(uid);
        ArrayList<OrderForm> orderFormList = orderformDao.getOrderFormByUid(uid);
        ArrayList<OrderFormVo> orderFormVoList = new ArrayList<OrderFormVo>();

        if (!orderFormList.isEmpty()) {
            for (OrderForm of : orderFormList) {

                int cid = of.getClassid();
                ClassInfo ci = classDao.getClassInfoByCid(cid);

                int tid = ci.getTeacherid();
                Teacher t = teacherDao.getTeacherByTid(tid);

                OrderFormVo ofv = new OrderFormVo(t, ci, of);
                orderFormVoList.add(ofv);
            }
        }

        return orderFormVoList;
    }


    /**
     * desc：通过订单ID支付订单
     */
    public int payOrderFormByOid(Integer oid) {

        OrderForm of = orderformDao.getOrderFormByOid(oid);
        boolean flag = isPayable(of.getClassid(), of.getUserid());

        if (flag) {
            int result = orderformDao.payOrderFormByOid(oid);
            if (result == 0) {
                return 1;
            }
            return 0;
        }
        return 1;


    }


    /**
     * desc：通过订单ID数组批量支付订单
     */
    public int payOrderFormByOidList(ArrayList<Integer> oidList) {


        ArrayList<OrderForm> ofList = new ArrayList<OrderForm>();
        for (int oid : oidList) {
            OrderForm of = orderformDao.getOrderFormByOid(oid);
            ofList.add(of);
        }

        if (inspectSelf(ofList)) {
            int isSuccess = 0;
            for (Integer oid : oidList) {
                if (payOrderFormByOid(oid) == 1) {
                    isSuccess = 1;
                }
            }
            return isSuccess;
        } else {
            return 1;
        }


    }


    /**
     * desc：通过用户ID支付购物车内所有订单
     */
    public int payShoppingCartByUid(Integer uid) {
        int result = orderformDao.payShoppingCartByUid(uid);
        if (result == 0) {
            return 1;
        }
        return 0;
    }


    /**
     * desc：通过用户ID和课程ID新增一个订单（未支付）
     */
    public int addOrderFormByOrderForm(Integer cid, Integer uid, Integer ispay) {
        OrderForm of = new OrderForm();
        of.setOrdertime(new Date());
        of.setClassid(cid);
        of.setUserid(uid);
        of.setIspay(ispay);
        if (ispay == 1) {

            boolean flag = isPayable(of.getClassid(), of.getUserid());
            if (flag) {
                int result = orderformDao.addOrderFormByOrderForm(of);
                if (result == 0) {
                    return 1;
                }
                return 0;
            }
            return 1;


        } else {
            int result = orderformDao.addOrderFormByOrderForm(of);
            if (result == 0) {
                return 1;
            }
            return 0;
        }
    }

    public int deleteOrderFormByOid(Integer oid) {
        int result = orderformDao.deleteOrderFormByOid(oid);
        if (result == 0) {
            return 1;
        }
        return 0;


    }

    public int deleteOrderFormByOidList(ArrayList<Integer> oidList) {
        int result = orderformDao.deleteOrderFormByOidList(oidList);
        if (result == 0) {
            return 1;
        }
        return 0;
    }


    /**
     * desc：通过用户ID获取该用户购物车内的订单（所有未支付订单）
     */
    public ArrayList<OrderFormVo> getShoppingCartByUid(Integer uid) {
        //ArrayList<Integer> cidList = orderformDao.getOrderFormCidByUid(uid);
        ArrayList<OrderForm> orderFormList = orderformDao.getShoppingCartByUid(uid);
        ArrayList<OrderFormVo> orderFormVoList = new ArrayList<OrderFormVo>();

        if (!orderFormList.isEmpty()) {
            for (OrderForm of : orderFormList) {

                int cid = of.getClassid();
                ClassInfo ci = classDao.getClassInfoByCid(cid);

                int tid = ci.getTeacherid();
                Teacher t = teacherDao.getTeacherByTid(tid);

                OrderFormVo ofv = new OrderFormVo(t, ci, of);
                orderFormVoList.add(ofv);
            }
        }

        return orderFormVoList;
    }

    /**
     * 查询单个订单是否可以被支付
     */
    private boolean isPayable(int cid, int uid) {

        //1.获取当前需要插入的订单的时间集（仅判断一个）
        ClassInfo ciNow = classDao.getClassInfoByCid(cid);

        //2.获取已支付订单中比较对象的数组
        //已支付订单比较对象数组
        ArrayList<Integer> havePayJudgementList = new ArrayList<Integer>() {
        };
        //已支付全部订单
        ArrayList<OrderForm> orderFormList = orderformDao.getOrderFormByUid(uid);
        //遍历已支付订单数组,提取所有课程时间进时间集，并将每个时间集加入时间集数组
        if (!orderFormList.isEmpty()) {
            for (OrderForm of : orderFormList) {
                //已支付订单时间集
                ClassInfo ci = classDao.getClassInfoByCid(of.getClassid());
                //已支付订单比较对象
                ArrayList<Integer> havePayJudgement = getJudgement(ci.getWeek(), ci.getLessontime());
                for (int judgement : havePayJudgement) {
                    havePayJudgementList.add(judgement);
                }
            }
        } else if (orderFormList.isEmpty()) {
            return true;
        }


        //3.输入数据进行比较
        ArrayList<Integer> nowTimeJudgementList = getJudgement(ciNow.getWeek(), ciNow.getLessontime());

        return timeJudge(nowTimeJudgementList, havePayJudgementList);

    }


    /**
     * 自我检查订单列表中是否有冲突
     **/
    private boolean inspectSelf(ArrayList<OrderForm> ofList) {
        OrderForm firstOf = ofList.get(0);
        ofList.remove(0);

        ClassInfo firstci = classDao.getClassInfoByCid(firstOf.getClassid());
        ArrayList<Integer> firstJudgementList = getJudgement(firstci.getWeek(), firstci.getLessontime());
        ArrayList<Integer> othersJudgementList = new ArrayList<Integer>();

        if (!ofList.isEmpty()) {
            for (OrderForm of : ofList) {
                //已支付订单时间集
                ClassInfo ci = classDao.getClassInfoByCid(of.getClassid());
                //已支付订单比较对象
                ArrayList<Integer> havePayJudgement = getJudgement(ci.getWeek(), ci.getLessontime());
                for (int judgement : havePayJudgement) {
                    othersJudgementList.add(judgement);
                }
            }
        }


        return timeJudge(firstJudgementList, othersJudgementList);

    }


    /**
     * 比较函数
     */
    private boolean timeJudge(ArrayList<Integer> sourceJudgementList, ArrayList<Integer> targetJudgementList) {
        boolean isOK = true;

        for (int nowTimeJudgement : sourceJudgementList) {
            for (int havePayJudgement : targetJudgementList) {
                if (nowTimeJudgement == havePayJudgement) {
                    isOK = false;
                }
            }
        }
        return isOK;

    }


    /**
     * 根据week和leesontime 获得比较对象数组
     */
    private ArrayList<Integer> getJudgement(int week, int lessontime) {

        String weekStr = String.valueOf(week);
        String lessontimeStr = String.valueOf(lessontime);
        ArrayList<Integer> judgementList = new ArrayList<Integer>();
        for (int i = 0; i < weekStr.length(); i++) {
            Integer judgement = Integer.valueOf(weekStr.substring(i, i + 1)) * 10 + Integer.valueOf(lessontimeStr.substring(i, i + 1));

            judgementList.add(judgement);
        }
        return judgementList;
    }


}
